/**
 * 〈功能简述〉<br>
 * 〈user逻辑实现层〉
 *
 * @author LiJunYi
 * @create 2019/7/3
 * @since 1.0.0
 */
package com.ljy.system.marvel_sys.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ljy.system.marvel_sys.system.dao.DepartmentDao;
import com.ljy.system.marvel_sys.system.dao.UserDao;
import com.ljy.system.marvel_sys.system.dao.UserRoleDao;
import com.ljy.system.marvel_sys.system.model.department.Department;
import com.ljy.system.marvel_sys.system.model.role.Role;
import com.ljy.system.marvel_sys.system.model.user.User;
import com.ljy.system.marvel_sys.system.model.user.UserRole;
import com.ljy.system.marvel_sys.system.service.IUserService;
import com.ljy.system.marvel_sys.common.util.md5.Md5Util;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class UserServiceImpl implements IUserService {

    @Resource
    UserDao userDao;
    @Resource
    DepartmentDao departmentDao;
    @Resource
    UserRoleDao userRoleDao;

    @Override
    public IPage<User> queryUser(String userName, String departmentName, int limit, int pages) {
        Department dep = departmentDao.selectOne(new QueryWrapper<Department>().lambda().eq(Department::getDepartmentName, departmentName));
        if(null != dep){
            int  depId = (int)dep.getId();
            Page<User> page = new Page<>(pages,limit,true);
            IPage<User> users = userDao.selectPage(page,new QueryWrapper<User>()
                    .like("user_name", userName)
                    .eq("department_id",depId)
                    .orderByDesc("id"));
            return users;
        }else{
            Page<User> page = new Page<>(pages,limit,true);
            IPage<User> users = userDao.selectPage(page,new QueryWrapper<User>()
                    .like("user_name", userName)
                    .orderByDesc("id"));
            return users;
        }
    }

    @Override
    public User queryByUserName(String userName) {
        User user = userDao.selectOne(new QueryWrapper<User>().lambda().eq(User::getUserName, userName));
        return user;
    }

    @Override
    public List<Role> getRoles(String name) {
        return userDao.getRoles(name);
    }

    @Override
    public List<Role> getRoles(int userId) {
        return userDao.getRolesByUserId(userId);
    }

    /**
    * 注册用户
    * */
    @Override
    public String insertUser(User user) {
        // 检查是否存在该用户
        User u = userDao.selectOne(new QueryWrapper<User>().lambda().eq(User::getUserName, user.getUserName()));
        if(null == u){
            //密码进行加密处理
            String psw = Md5Util.md5Password(user.getUserPassword(),user.getUserName());
            user.setUserPassword(psw);
            user.setUserImg("sysDefault.jpg");
            int i = userDao.insert(user);
            if(i > 0){
                // 为新增成功的用户分配角色，默认用户角色
                UserRole userRole = new UserRole();
                userRole.setUserId(user.getId());
                userRole.setRoleId(3);
                userRoleDao.insert(userRole);
                return "ok";
            }else{
                return "注册失败!";
            }
        }else{
            return "该用户已存在!";
        }
    }

    @Override
    public int updateUserPsw(User user) {
        return userDao.update(
                null,
                Wrappers.<User>lambdaUpdate().set(User::getUserPassword, user.getUserPassword()).eq(User::getId, user.getId()));
    }

    @Override
    public int updateUserImg(User user) {
        return userDao.update(null,
                Wrappers.<User>lambdaUpdate().set(User::getUserImg, user.getUserImg() ).eq(User::getId, user.getId()));
    }

    @Override
    public int updateUser(User user) {
        return userDao.update(null,Wrappers.<User>lambdaUpdate().
                set(User::getNickName, user.getNickName()).
                set(User::getUserPhone, user.getUserPhone()).
                set(User::getUserEmail, user.getUserEmail()).
                set(User::getUserSex,user.getUserSex()).
                set(User::getUserNote, user.getUserNote()).
                eq(User::getId, user.getId()));
    }

    @Override
    public int updateUserType(User user) {
        return userDao.update(null, Wrappers.<User>lambdaUpdate().
                set(User::getUserType, user.getUserType() ).
                eq(User::getId, user.getId()));
    }

    @Override
    public int updateUserDep(long id, long depId) {
        return userDao.update(null, Wrappers.<User>lambdaUpdate().
                set(User::getDepartmentId,depId ).
                eq(User::getId, id));
    }
}
